﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Gyroscope : MonoBehaviour {
    public Text info;
    private float speed = 0.2f;
    UnityEngine.Gyroscope gyro;
    bool gyinfo;
    Quaternion quatMap;
    Quaternion quatMult = new Quaternion(0, 0, 1, 0);

    protected void Start()
    {
        //设置设备陀螺仪的开启/关闭状态，使用陀螺仪功能必须设置为 true    
        Input.gyro.enabled = true;
        //获取设备重力加速度向量    
        Vector3 deviceGravity = Input.gyro.gravity;
        //设备的旋转速度，返回结果为x，y，z轴的旋转速度，单位为（弧度/秒）    
        Vector3 rotationVelocity = Input.gyro.rotationRate;
        //获取更加精确的旋转    
        Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;
        //设置陀螺仪的更新检索时间，即隔 0.1秒更新一次    
        Input.gyro.updateInterval = 0.1f;
        //获取移除重力加速度后设备的加速度    
        Vector3 acceleration = Input.gyro.userAcceleration;
    }

    private void Update()
    {
        //if (transform.rotation.y > -0.33f && transform.rotation.y < 0.33f)
        {
            /*
            quatMap = new Quaternion(gyro.attitude.x, gyro.attitude.y, gyro.attitude.z, gyro.attitude.w);
            Quaternion qt = quatMap * quatMult;
            //在localRotation和qt之间的插值  
            transform.localRotation = Quaternion.Slerp(transform.localRotation, qt, speed);
            */
            Quaternion qt = Input.gyro.attitude * quatMult;
            transform.rotation = Input.gyro.attitude* quatMult;
            Debug.Log("dsadsa");
            info.text = qt.ToString();
        }
    }

    private static Quaternion GyroToUnity(Quaternion q)
    {
        return new Quaternion(q.x, q.y, -q.z, -q.w);
    }

}
